<?php
/**
 * 刹车片 - 数据分析
 */

namespace app\logic\F6DataAnalysis;

use app\server\SendRequestServer;
use think\Exception;

class BrakePadsData implements DataBaseInterface
{
    // 前刹车片
    public function analysis($data, $vin,  $room_info = [])
    {
        try {
            $afterData = $this->getAfterData($vin);

            $before = $this->getDataWithRes($data, '前刹车片');
            $after = $this->getDataWithRes($afterData, '后刹车片');
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }

        return [$before, $after];

    }

    public function buildMessage($vin, $car_name, $show)
    {
        // TODO: Implement buildMessage() method.
        return '【查询结果】' . ' \n ' . '🌟' . $vin . '🌟' . ' \n\n ' . '🚘 车名：' . $car_name . ' \n\n ' . $show;
    }

    // 后刹车片
    public function getAfterData($vin)
    {
        try {
            $res = (new SendRequestServer())->send($vin, [
                'module_id' => 19,
                'key' => '后刹车片',
                'data_type' => '底盘悬挂系统'
            ]);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }

        // 开始处理，返回后
        return $res['data']['后刹车片'];
    }

    public function getDataWithRes($data, $name)
    {
        $proArr = [];
        foreach ($data as $k => $v) {
            $text = '';

            if (!isset($v['不同品牌的数据'])) {
                throw new Exception('数据结构错误-3');
            }

            foreach ($v['不同品牌的数据'] as $vv) {
                foreach ($vv as $kkk => $vvv) {

                    // 品牌过滤
                    if (!in_array($kkk,  ['博世/BOSCH']))  {
                        continue;
                    }

                    $key = $kkk;
                    $currentPro = $vvv['value'];

                    $obj = '';
                    foreach ($vvv['body'] as $item) {
                        $obj .= $item['attrName'] . '：' . $item['attrValue'] . ' \n ';
                    }

                    $obj = !empty($obj) ? $obj : '--' . ' \n ';

                    $text .= '🔖 ' . $name . '品牌：' . $key . ' \n ' . $name . '型号：' . $currentPro . ' \n ' . '具体参数：' . ' \n ' . $obj . '======================== \n ';
                }

            }

            $proArr[$k] = $text ?? '未查询到可用数据！';
        }

        return $proArr;
    }
}